home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 September / PCWorld_2008-09_cd.bin / v cisle / sadanastroju / wot-20080519-fx.xpi / chrome / wot.jar / content / api / reload.js < prev    next >
Text File  |  2008-03-30  |  3KB  |  130 lines

  1. /*
  2.     api/reload.js
  3.  
  4.     Copyright ┬⌐ 2005, 2006, 2007  Against Intuition, Inc. <info@mywot.com>
  5. */
  6.  
  7. const WOT_RELOAD_RUNNING = "wot_reload_running";
  8.  
  9. var wot_api_reload =
  10. {
  11.     init: function()
  12.     {
  13.         this.timeout = null;
  14.     },
  15.  
  16.     send: function(reload)
  17.     {
  18.         try {
  19.             if (this.timeout) {
  20.                 window.clearTimeout(this.timeout);
  21.                 this.timeout = null;
  22.             }
  23.  
  24.             if (!wot_api_register.ready ||
  25.                     !wot_api_register.geteid() ||
  26.                     wot_hashtable.get(WOT_RELOAD_RUNNING)) {
  27.                 return;
  28.             }
  29.  
  30.             wot_hashtable.set(WOT_RELOAD_RUNNING, 1);
  31.  
  32.             var query_string = WOT_SERVICE_API_RELOAD +
  33.                 "?id="        + wot_prefs.witness_id +
  34.                 "&nonce="     + wot_crypto.nonce() +
  35.                 "&reload="     + encodeURIComponent(reload) +
  36.                 "&eid="        + wot_prefs.extension_id +
  37.                 "&lang="    + wot_util.getstring("language") +
  38.                 "&version="    + WOT_PLATFORM + "-" + WOT_VERSION;
  39.  
  40.             var request = new XMLHttpRequest();
  41.  
  42.             request.open("GET", WOT_SERVICE_SECURE +
  43.                 wot_crypto.authenticate_query(query_string));
  44.  
  45.             new wot_cookie_remover(request);
  46.  
  47.             request.onload = this.onload;
  48.             request.send(null);
  49.         } catch (e) {
  50.             dump("wot_reload.send: failed with " + e + "\n");
  51.             this.error();
  52.         }
  53.     },
  54.  
  55.     onload: function(event)
  56.     {
  57.         try {
  58.             if (!event || !event.target || event.target.status != 200 ||
  59.                     !event.target.responseXML) {
  60.                 wot_api_reload.error();
  61.                 return;
  62.             }
  63.  
  64.             var reload = null;
  65.             var tags = event.target.responseXML.getElementsByTagName(
  66.                             WOT_SERVICE_XML_RELOAD);
  67.  
  68.             if (tags) {
  69.                 reload = tags.item(0);
  70.             }
  71.  
  72.             if (!reload || !reload.attributes) {
  73.                 wot_api_reload.error();
  74.                 return;
  75.             }
  76.  
  77.             var id  = reload.attributes.getNamedItem(
  78.                             WOT_SERVICE_XML_RELOAD_ID);
  79.             var key = reload.attributes.getNamedItem(
  80.                             WOT_SERVICE_XML_RELOAD_KEY);
  81.  
  82.             if (!id || !id.nodeValue || !key || !key.nodeValue ||
  83.                 id.nodeValue.length  != WOT_LENGTH_WITNESS_ID ||
  84.                 key.nodeValue.length != WOT_LENGTH_WITNESS_KEY) {
  85.                 wot_api_reload.error();
  86.                 return;
  87.             }
  88.  
  89.             if (!wot_prefs.setChar("witness_id", id.nodeValue) ||
  90.                 !wot_prefs.setChar("witness_key", key.nodeValue)) {
  91.                 wot_api_reload.error();
  92.                 return;
  93.             }
  94.  
  95.             wot_my_session.update(false);
  96.  
  97.             /* Invalidate cache */
  98.             var cache = wot_cache.get_enumerator();
  99.  
  100.             while (cache.hasMoreElements()) {
  101.                 var name = wot_cache.get_name_from_element(cache.getNext());
  102.                 if (name) {
  103.                     wot_cache.set(name, "status", WOT_QUERY_RETRY);
  104.                 }
  105.             }
  106.  
  107.             wot_core.update();
  108.             wot_hashtable.remove(WOT_RELOAD_RUNNING);
  109.         } catch (e) {
  110.             dump("wot_reload.onload: failed with " + e + "\n");
  111.             wot_api_reload.error();
  112.         }
  113.     },
  114.  
  115.     error: function()
  116.     {
  117.         try {
  118.             wot_api_reload.timeout =
  119.                 window.setTimeout(wot_api_reload.send,
  120.                     WOT_INTERVAL_RELOAD_ERROR);
  121.  
  122.             wot_hashtable.remove(WOT_RELOAD_RUNNING);
  123.         } catch (e) {
  124.             dump("wot_reload.error: failed with " + e + "\n");
  125.         }
  126.     }
  127. };
  128.  
  129. wot_api_reload.init();
  130.